import pymysql

# 创建数据库连接对象
dbconn = pymysql.connect(host="localhost", port=3306,
                         user='root', password='123456', database='girls')
# 根据连接对象，创建游标对象  执行sql语句的
cur = dbconn.cursor()
# 执行sql语句
cur.execute("select * from boys order by id desc")
result = cur.fetchall()  # 返回全部数据 以元组的形式
print(result)
for a in result:
    print(a)
print(type(result))
# 查询一条数据
cur.execute("select * from boys where id =1  order by id desc")
reultOne = cur.fetchone()  # 返回第一条数据 以元组的形式
print(reultOne)
# 修改数据
cur.execute('UPDATE  boys set boyName="张三" WHERE id =1')
dbconn.commit()  # 事务提交  事务在对数据进行修改时才会参与，此时我们需要commit()
# db.rollback() #3回滚
# 增加数据
cur.execute("insert into boys(boyName,userCP) values('吕布',10000)")
dbconn.commit()
# 删除数据
cur.execute("delete from boys where id=99")
dbconn.commit()

cur.close()  # 关闭游标对象
dbconn.close()  # 关闭连接对象
